﻿#Region "Copyright"
'————————————————————————————————————————
'	创建人员(Creator)    ：Huangyd
'	创建日期(Date)       ：2012-07-14
'   联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
'   描    述(Description)：
'
'	Copyright (C) 2012 ajayumi 版权所有
'————————————————————————————————————————
#End Region
Imports System.Text.RegularExpressions
Imports System.Net.Mail
Imports System.ComponentModel
Imports System.Net

Namespace Net.Email

    ''' <summary>
    ''' Email 处理类
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Email

#Region "property"

        Private m_EmailInfo As EmailInfo = Nothing
        Public Property EmailInfo() As EmailInfo
            Get
                Return m_EmailInfo
            End Get
            Set(ByVal value As EmailInfo)
                m_EmailInfo = value
            End Set
        End Property


        Private m_Message As String = String.Empty
        ''' <summary>
        ''' 反馈信息
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Message() As String
            Get
                Return m_Message
            End Get
            Set(ByVal value As String)
                m_Message = value
            End Set
        End Property

#End Region

#Region "constructed function"

        Public Sub New(ByVal model As EmailInfo)
            Me.m_EmailInfo = model
        End Sub

#End Region

#Region "event"

        ''' <summary>
        ''' 发生邮件完成事件
        ''' </summary>
        ''' <remarks></remarks>
        Public Event SendCompleted As AsyncCompletedEventHandler
        Public Overridable Sub OnSendCompleted(ByVal e As AsyncCompletedEventArgs)
            If e IsNot Nothing Then RaiseEvent SendCompleted(Me, e)
        End Sub

#End Region

#Region "private method"

        Private Sub send(ByVal _Email As MailMessage)
            Dim _SmtpClient As SmtpClient = Nothing
            Try
                _SmtpClient = New SmtpClient(Me.m_EmailInfo.Host, Me.m_EmailInfo.Port)
                With _SmtpClient
                    .EnableSsl = Me.m_EmailInfo.IsEnableSsl
                    .Credentials = New NetworkCredential(Me.m_EmailInfo.Sender, Me.m_EmailInfo.UserPwd)

                    AddHandler .SendCompleted, AddressOf Me.sendCompletedCallback
                    .SendAsync(_Email, Nothing)
                End With

            Catch ex As SmtpException
                Me.OnSendCompleted(New AsyncCompletedEventArgs(ex, True, Nothing))
            Catch ex As Exception
                Me.OnSendCompleted(New AsyncCompletedEventArgs(ex, True, Nothing))
            Finally
                _SmtpClient = Nothing
            End Try
        End Sub

        Private Sub sendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
            If e.Cancelled Then
                Me.Message = String.Format("发给{0}的邮件已经被取消", Me.m_EmailInfo.Receiver)
            End If

            Me.OnSendCompleted(e)
        End Sub

#End Region

#Region "public method"

        ''' <summary>
        ''' 发送邮件
        ''' </summary>
        Public Sub SendEmail()
            Dim _MailMessage As MailMessage = Nothing
            Try
                _MailMessage = New MailMessage()
                With _MailMessage
                    .To.Add(Me.m_EmailInfo.Receiver)
                    .From = New MailAddress(Me.m_EmailInfo.Sender)
                    .Subject = Me.m_EmailInfo.Subject
                    .Body = Me.m_EmailInfo.Body
                    .SubjectEncoding = Me.m_EmailInfo.Encoding
                    For Each s As String In Me.m_EmailInfo.AttachmentList
                        .Attachments.Add(New Attachment(s))
                    Next
                End With

                Me.send(_MailMessage)
            Catch ex As SmtpException
                Me.OnSendCompleted(New AsyncCompletedEventArgs(ex, True, Nothing))
            Catch ex As Exception
                Me.OnSendCompleted(New AsyncCompletedEventArgs(ex, True, Nothing))
            Finally
                _MailMessage = Nothing
            End Try
        End Sub

#End Region

#Region "shared mothod"

        ''' <summary>
        ''' 邮箱格式判断
        ''' </summary>
        ''' <returns>验证成功返回True</returns>
        Public Shared Function isEmailFormat(ByVal _EmailStr As String) As Boolean
            '收件人输入框判断
            Return Regex.IsMatch(_EmailStr, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        End Function

#End Region


    End Class
End Namespace

